package com.bifang.module.file.dao;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.bifang.module.system.core.dao.EnhanceFreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 自动生成代码配置
 * @author: chenzh
 */
public class GenerateMybatisPlus {

    public static void main(String[] args) {
        // 需要配置的字段
        String url =
                "jdbc:mysql://118.31.228.198:3310/bifang_file?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
        String username = "bifang_file";
        String password = "epsoft@123456";

        String author = "xyh";
        String[] tables = {"file_collect_data"};
        // service,mapper,xml,model生成的项目路径
        String outputDir =
                System.getProperty("user.dir") + "/fileService-core/src/main/java";
        // service,mapper,xml,model父包名
        String packageName = "com.bifang.module.file";
        // vo生成的包路径
        String voDir =
                System.getProperty("user.dir") + "/fileService-common/src/main/java";
        String voPackageName = "com.bifang.module.file.common.vo";
        String integrationDir =
                System.getProperty("user.dir") + "/fileService-core/src/main/java";
        String integrationPackageName = "com.bifang.module.file.core.biz";
        String controllerDir =
                System.getProperty("user.dir") + "/fileService-core/src/main/java";
        String controllerPackageName = "com.bifang.module.file.gateway.controller";

        String funcCode = "fileCollectData";
        String controllerTitle = "文件信息表";

        FastAutoGenerator.create(url, username, password)
                .globalConfig(
                        builder -> {
                            builder.author(author) // 设置作者
                                    .enableSwagger() // 开启 swagger 模式
                                    .outputDir(outputDir) // 指定输出目录
                                    .disableOpenDir() // 禁止打开输出目录
                                    .dateType(DateType.ONLY_DATE);
                        })
                .packageConfig(
                        builder -> {
                            Map<OutputFile, String> pathMap = new HashMap<>();
                            pathMap.put(
                                    OutputFile.other,
                                    voDir
                                            + "/"
                                            + voPackageName.replace(".", "/")
                                            + ","
                                            + integrationDir
                                            + "/"
                                            + integrationPackageName.replace(".", "/")
                                            + ","
                                            + controllerDir
                                            + "/"
                                            + controllerPackageName.replace(".", "/"));
                            builder.parent(packageName) // 设置父包名
                                    .entity("dao.model")
                                    .mapper("dao.mapper")
                                    .xml("dao.mapping")
                                    .service("dao.service")
                                    .serviceImpl("dao.service.impl")
                                    //
                                    // .moduleName(controllerPath) // 设置父包模块名
                                    .pathInfo(pathMap);
                        })
                .strategyConfig(
                        builder -> {
                            builder.addInclude(tables) // 设置需要生成的表名
                                    .serviceBuilder()
                                    .fileOverride()
                                    .formatServiceFileName("%sService")
                                    .entityBuilder()
                                    .fileOverride()
                                    .enableLombok()
                                    .enableChainModel()
                                    .logicDeleteColumnName("DEL")
                                    .logicDeletePropertyName("del")
                                    .versionColumnName("VERSION")
                                    .versionPropertyName("version")
                                    .enableTableFieldAnnotation()
                                    .addTableFills(new Column("create_time", FieldFill.INSERT))
                                    .addTableFills(new Column("create_name", FieldFill.INSERT))
                                    .addTableFills(
                                            new Column("update_time", FieldFill.INSERT_UPDATE))
                                    .addTableFills(
                                            new Column("update_name", FieldFill.INSERT_UPDATE))
                                    .addTableFills(new Column("del", FieldFill.INSERT))
                                    .addTableFills(new Column("version", FieldFill.INSERT))
                                    .mapperBuilder()
                                    .fileOverride()
                                    .enableMapperAnnotation();
                            // .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                        })
                .templateConfig(
                        builder -> {
                            builder.entity("/templatesFreemaker/entity.java")
                                    .service("/templatesFreemaker/service.java")
                                    .serviceImpl("/templatesFreemaker/serviceImpl.java")
                                    .mapper("/templatesFreemaker/mapper.java")
                                    .xml("/templatesFreemaker/mapper.xml")
                                    .disable(TemplateType.CONTROLLER)
                                    .build();
                        })
                .injectionConfig(
                        consumer -> {
                            Map<String, String> customFile = new HashMap<>();
                            Map<String, Object> customMap = new HashMap<>();
                            customFile.put("VO", "/templatesFreemaker/VO.java.ftl");
                            customFile.put("KeyVO", "/templatesFreemaker/keyVO.java.ftl");
                            customFile.put("QueryVO", "/templatesFreemaker/queryVO.java.ftl");
                            customFile.put(
                                    "Integration", "/templatesFreemaker/integration.java.ftl");
                            customFile.put(
                                    "IntegrationImpl",
                                    "/templatesFreemaker/integrationImpl.java.ftl");
                            customFile.put("Controller", "/templatesFreemaker/controller.java.ftl");
                            customMap.put("voPackageName", voPackageName);
                            customMap.put("integrationPackageName", integrationPackageName);
                            customMap.put("controllerPackageName", controllerPackageName);
                            customMap.put("funcCode", funcCode);
                            customMap.put("controllerTitle", controllerTitle);
                            consumer.customFile(customFile);
                            consumer.customMap(customMap);
                            consumer.fileOverride();
                        })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new EnhanceFreemarkerTemplateEngine())
                .execute();
    }
}
